package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.pojo.Department;
import com.mycom.pojo.Item;
import com.mycom.pojo.WorkType;
import com.mycom.service.DepartmentService;
import com.mycom.service.ItemService;
import com.mycom.service.WorkTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/departments")
public class DepartmentController {
    @Autowired
    private WorkTypeService workTypeService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public Result findAll(){
        List<Department> departments = departmentService.findAll();
        List<WorkType> workTypes = workTypeService.findAll();
        List<Item> items = itemService.findAll();
        for (WorkType workType : workTypes) {
            List<Item> itemList = new ArrayList<>();
            for (Item item : items) {
                if (workType.getId().equals(item.getWorkTypeId())){
                    itemList.add(item);
                }
            }
            workType.setChildren(itemList);
        }
        for (Department department : departments) {
            List<WorkType> workTypeList = new ArrayList<>();
            for (WorkType workType : workTypes) {
                if (workType.getDepartmentId().equals(department.getId())){
                    workTypeList.add(workType);
                }
            }
            department.setChildren(workTypeList);
        }
        return new Result(Code.QUERY_ALL_SUCCESS,departments,"查询所有成功！");
    }
}
